package controllerlayer;
import java.util.Calendar;
import java.sql.Timestamp;
import java.util.Date;
/**
 * This class handles all the timestamp issuses.
 * TimestampHandler will make it easy for you to
 * shift between real date format and timestamp
 * format. You are welcome :-)
 * 
 * @author Stefan Bols 
 * @version 22/04-2013
 */
public class TimestampCtrl
{
    // OOOOBS! The currentTime will not always hold the real current time, but it
    //          hold the time that we are currently processing!
    private Calendar currentTime = Calendar.getInstance();
    private long tsJava;
    private long tsPHP;

    /**
     * Construcs current timestamp in Java and PHP format
     */
    public TimestampCtrl()
    {
        tsJava = currentTime.getTime().getTime();
        tsPHP = tsJava/1000;
    }

    
    /**
     * Construcs specified timestamp in Java and PHP format
     * From that date and time that you decide
     * 
     * @param year      The year
     * @param month     The month (1-12
     * @param day       The day (1-31)
     * @param hour      The hour (0-23)
     * @param minute    The minute (0-59)
     * @param seconds    The second (0-59)
     */
    public TimestampCtrl(int year, int month, int day, int hour, int minute)
    {
        // Here we are setting the Calendar to our specified data and time
        //  It's correct that month is month-1 - january is 0 in the Calendar class
        currentTime.set(year,month-1,day,hour,minute);
        tsJava = currentTime.getTime().getTime();
        tsPHP = tsJava/1000;
    }

    
    /**
     * Construcs specified timestamp in Java and PHP format
     * From a timestamp
     */
    public TimestampCtrl(long pTimestamp, boolean phpformat)
    {
        long timestamp;
        
        if (phpformat)
        {
            timestamp = (pTimestamp*1000);
        } else
        {
            timestamp = pTimestamp;
        }
        
        Timestamp stamp = new Timestamp(timestamp);
        Date date = new Date(stamp.getTime());
        currentTime.setTime(date);
        
        tsJava = currentTime.getTime().getTime();
        tsPHP = tsJava/1000;
    }
    
    
    /**
     * Returning timestamp for PHP
     * 
     * @return Timestamp in PHP type (seconds)
     */
    public long getTimestampPHP()
    {
        return tsPHP;
    }
    
    
    /**
     * Returning timestamp for Java
     * 
     * @return Timestamp in Java type (milliseconds)
     */
    public long getTimestampJava()
    {
        return tsJava;
    }
    
    
    /**
     * Returning current year
     * 
     * @return Year
     */
    public int getYear()
    {
        return currentTime.get(currentTime.YEAR);
    }
    
    
    /**
     * Returning current month
     * 
     * @return Month (1 = January, 12 = December)
     */
    public int getMonth()
    {
        return (currentTime.get(currentTime.MONTH)+1);
    }
    
    
    /**
     * Returning current day of the month
     * 
     * @return Day of the month
     */
    public int getDay()
    {
        return currentTime.get(currentTime.DAY_OF_MONTH);
    }
    
    
    /**
     * Returning current hour
     * 
     * @return Hour of the day
     */
    public int getHour()
    {
        return currentTime.get(currentTime.HOUR_OF_DAY);
    }
    
    
    /**
     * Returning current minute
     * 
     * @return Minute
     */
    public int getMinute()
    {
        return currentTime.get(currentTime.MINUTE);
    }
    
    
    /*
     * Returning current seconds
     * 
     * @return Seconds
     
    public int getSeconds()
    {
        return currentTime.get(currentTime.SECOND);
    }
    */
    
    /**
     * Creating a leading zero, if one is needed and returns the number as string.
     * 
     * @return Number with or without a leading zero
     */
    public String setLeadingZero(int time)
    {
        // 00 01 02 03 .... 08 09 10 11 ..... 
        String returnTime = "";
        
        if (time>=0 && time < 10) {
            returnTime = "0";
        }
        
        returnTime += time;
        
        return returnTime;
    }
    
    
    /**
     * Returning full current time, format: H:m or H:m:s
     * 
     * @param   Seconds
     * @return  Time
     */
    public String getTime(boolean seconds)
    {
        String time = setLeadingZero(getHour())+":"+setLeadingZero(getMinute());
        /*if (seconds)
        {
            time += setLeadingZero(getSeconds());
        }*/
        
        return time;
    }
    
    /**
     * Returning full current date string, format: YYYY-M-D H:m:s
     * 
     * @return Current date and time as String, format: YYYY-M-D H:m:s
     */
    public String getDateTimeString()
    {
        return getYear()+"-"+setLeadingZero(getMonth())+"-"+setLeadingZero(getDay())+" "+getTime(true);
    }
}
